package com.sporch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * @Author: czy
 * @Date: 2019/6/4 17:53
 */
public class Util {
    private static final Logger LOG = LoggerFactory.getLogger(Util.class);
    /**
     * 系统自带MD5
     * 默认使用大写
     * @param pInput
     * @return
     */
    public static  String getMD5(String pInput) {
        try {
            MessageDigest lDigest = MessageDigest.getInstance("MD5");
            lDigest.update(pInput.getBytes());
            BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (NoSuchAlgorithmException lException) {
            throw new RuntimeException(lException);
        }
    }

    /**
     * 读取控制台输入字符串
     * @return string, that  user input in console
     */
    public static String getUserInput() {
        Scanner scanner = new Scanner(System.in);
        String userInput = scanner.nextLine();
        return userInput;
    }

    public static void sleepForSeconds(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            LOG.warn("Interrupted when thread was sleep for timeout", e);
        }
    }

    /*
     * Method, that emulate getting temperature from real sensor.
     * Retrieves random temperature.
     */
    public static int getTemperatureRand() {
        return new Random().nextInt(10) + 25;
    }
}
